﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RedmineHelper
{
    public partial class FormAccount : Form
    {
        public FormAccount()
        {
            InitializeComponent();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            IList<account> accounts = AccountHelper.LoadAccounts();
            var al = from a in accounts where a.host == txtHost.Text select a;
            if(al.Count()>0)
            {
                account ac = al.First();
                ac.username = txtUser.Text;
                ac.password = txtPass.Text;
                ac.closeid = int.Parse(txtStatus.Text);
            }
            else
            {
                account ac = new account();
                ac.host = txtHost.Text;
                ac.username = txtUser.Text;
                ac.password = txtPass.Text;
                ac.closeid = int.Parse(txtStatus.Text);
                accounts.Add(ac);
            }
            AccountHelper.SaveAccounts(accounts);
            this.Close();
        }

        public void SetText(account a)
        {
            this.txtHost.Text = a.host;
            this.txtUser.Text = a.username;
            this.txtPass.Text = a.password;
            this.txtStatus.Text = a.closeid.ToString();
        }
    }
}
